package com.fll.fangyitask.service.impl;

import com.fll.fangyicommon.entity.task.Task;
import com.fll.fangyicommon.vo.HotTaskVO;
import com.fll.fangyicommon.vo.TaskVO;
import com.fll.fangyitask.dao.LearnDao;
import com.fll.fangyitask.dao.TaskDao;
import com.fll.fangyitask.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author fll
 * @create 2023/3/9
 */
@Service
@Slf4j
public class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskDao taskDao;
    @Autowired
    private LearnDao learnDao;

    /**
     * 创建一条task
     * @param task
     * @return
     */
    @Override
    public Integer createTask(Task task) {
        Integer num = taskDao.createTask(task);
        if (num != 0){
            log.info("创建task成功===>{}", task);
            return num;
        }
        log.error("创建task失败");
        return null;
    }

    /**
     * 获取用户科目下的task
     * @param subjectId
     * @param userId
     * @return
     */
    @Override
    public List<Task> getUserTaskList(Integer subjectId, Integer userId){
        List<Task> userTaskList = taskDao.getUserTaskList(subjectId, userId);
        if (userTaskList.size() != 0){
            log.info("查询用户科目下task成功：userId===>{}，subjectId===>{}", userId,subjectId);
            return userTaskList;
        }
        log.error("查询用户科目下task失败，数据为空！");
        return null;
    }

    /**
     * 获取用户科目下的taskId
     * @param subjectId
     * @param userId
     * @return
     */
    public List<Integer> getUserTaskIdList(Integer subjectId, Integer userId){
        List<Integer> taskIdList = taskDao.getUserTaskIdList(subjectId, userId);
        if (taskIdList.size() != 0){
            return taskIdList;
        }
        return null;
    }

    /**
     * 修改task
     * @param task
     * @return
     */
    @Override
    public Integer editTask(Task task){
        Integer num = taskDao.editTask(task);
        if (num != 0){
            return num;
        }
        return -1;
    }

    /**
     * 删除task
     * 删除了n条数据,就返回n.
     * 删除一条返回1.
     * 删除2条返回2.
     * 删除0条就是删除0
     * @param taskId
     * @return
     */
    @Override
    public Integer deleteTask(Integer taskId){
        Integer num = taskDao.deleteTask(taskId);
        if (num != 0){
            log.info("删除task成功：taskId===>{}", taskId);
            return num;
        }
        log.error("task删除失败：taskId===>{}！",taskId);
        return -1;
    }

    /**
     * 根据taskTitle模糊查询,只返回一条，前端不想展示全部了
     * @param taskTitle
     */
    @Override
    public TaskVO selectTaskByTitle(String taskTitle){
        List<TaskVO> taskVO = taskDao.selectTaskByTitle(taskTitle);
        if (taskVO.size() != 0){
            log.info("模糊查询task成功：taskTitle===>{},task===>{}", taskTitle, taskVO);
            return taskVO.get(0);
        }
        log.error("模糊查询task失败：taskTitle===>{}，没有该title的task", taskTitle);
        return null;
    }


    /**
     * 获取10个热门task
     * @return
     */
    @Override
    public List<HotTaskVO> getHotTask(){
        List<HotTaskVO> hotTask = taskDao.getHotTask();
        if (hotTask != null && hotTask.size() != 0){
            log.info("热门task获取成功");
            return hotTask;
        }
        log.error("热门task为空");
        return null;
    }

}
